package com.lzhl.buildjar.ui;

import com.lzhl.buildjar.dao.CategoryDao;
import com.lzhl.buildjar.dao.DaoFactory;
import com.lzhl.buildjar.dao.SchemeDao;
import com.lzhl.buildjar.po.Category;
import com.lzhl.buildjar.po.Scheme;
import com.lzhl.buildjar.ui.support.BaseDialog;
import com.lzhl.buildjar.ui.support.OperationListener;
import com.lzhl.buildjar.util.FileUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2014/6/19
 */
public class SchemeEditUI extends BaseDialog {
    private JTextField tfParent;
    private JTextField tfName;

    private JButton btnOk;
    private JButton btnCancel;

    private Category parent;
    private Scheme me;

    private OperationListener listener;

    public SchemeEditUI(
            Frame owner, Category parent, Scheme me, OperationListener listener) {
        super(owner, me == null ? "新增方案" : (me.getId() <= 0 ? "复制方案" : "修改方案"), true);

        this.parent = parent;
        this.me = me;
        this.listener = listener;

        jbinit();
        init();

        setSize(400, 150);
        setLocationRelativeTo(owner);
        setVisible(true);
    }

    private void jbinit() {
        Container container = getContentPane();
        container.setLayout(new BorderLayout());

        Box boxMain = Box.createVerticalBox();
        boxMain.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        boxMain.setAlignmentX(Component.LEFT_ALIGNMENT);
        container.add(boxMain, BorderLayout.CENTER);

        // **************************************************
        Box boxParent = Box.createHorizontalBox();
        boxParent.setPreferredSize(new Dimension(60, 20));
        boxParent.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
        boxParent.setAlignmentX(Component.LEFT_ALIGNMENT);
        boxMain.add(boxParent);

        JLabel label= new JLabel("分类：");
        label.setPreferredSize(new Dimension(60, 20));
        boxParent.add(label);
        tfParent = new JTextField();
        tfParent.setEditable(false);
        boxParent.add(tfParent);

        boxMain.add(Box.createVerticalStrut(5));

        // **************************************************
        Box boxName = Box.createHorizontalBox();
        boxName.setPreferredSize(new Dimension(60, 20));
        boxName.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
        boxName.setAlignmentX(Component.LEFT_ALIGNMENT);
        boxMain.add(boxName);

        label= new JLabel("名称：");
        label.setPreferredSize(new Dimension(60, 20));
        boxName.add(label);
        tfName = new JTextField();
        boxName.add(tfName);

        Box boxCtrl = Box.createHorizontalBox();
        boxCtrl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        boxCtrl.setPreferredSize(new Dimension(60, 40));
        boxCtrl.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
        boxCtrl.setAlignmentX(Component.LEFT_ALIGNMENT);
        container.add(boxCtrl, BorderLayout.SOUTH);
        btnOk = new JButton("确定");
        btnCancel = new JButton("取消");
        boxCtrl.add(Box.createHorizontalGlue());
        boxCtrl.add(btnOk);
        boxCtrl.add(Box.createHorizontalStrut(5));
        boxCtrl.add(btnCancel);
    }

    private void init() {
        if(parent != null) {
            tfParent.setText(parent.getName());
        }
        if(me != null) {
            tfName.setText(me.getName());
        }

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                tfName.requestFocus();
            }
        });

        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnOk_actionPerformed(e);
            }
        });

        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                dispose();
            }
        });
    }

    private void btnOk_actionPerformed(ActionEvent e) {
        String name = tfName.getText();
        if(name == null || name.trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "名称不能为空！");
            return;
        }

        //文件名合法性检查
        name = name.trim();
        if(!FileUtils.isValidFileName(name)) {
            JOptionPane.showMessageDialog(this, "[" + name + "]不是一个有效的文件名！");
            return;
        }

        //修改方案但名称没有变化
        if(me != null && name.equals(me.getName())) {
            setVisible(false);
            dispose();
            return;
        }

        CategoryDao categoryDao = (CategoryDao) DaoFactory.getDao("categoryDao");
        if(categoryDao.isExists(parent, name)) {
            JOptionPane.showMessageDialog(this,
                    "父分类 [" + parent.getName() + "] 下已经存在同名的子分类 [" + name + "] ！");
            return;
        }

        SchemeDao schemeDao = (SchemeDao) DaoFactory.getDao("schemeDao");
        if(schemeDao.isExists(parent, name)) {
            JOptionPane.showMessageDialog(this,
                    "父分类 [" + parent.getName() + "] 下已经存在同名的方案 [" + name + "] ！");
            return;
        }

        if(me == null) {
            me = new Scheme();
            me.setCategory(parent);
            me.getClassitems().add("");
        }
        me.setName(name);

        setVisible(false);
        dispose();

        if(listener != null) {
            listener.operationPerformed(me);
        }
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception ignore) {
        }

        new SchemeEditUI((Frame)null, null, null, null);
    }
}
